﻿
Imports System.Runtime.InteropServices.Marshal
Imports System.Management

Namespace netstat
    Public Module _netstat
        Private _tcptable As MIB_TCPTABLE = Nothing

        Public Function MIBTcpTable() As MIB_TCPTABLE
            Return _tcptable
        End Function

        Public Function refreshMIBTcpTable() As Boolean
            If _tcptable Is Nothing Then
                _tcptable = New MIB_TCPTABLE()
            End If

            Dim tcptableData As IntPtr
            tcptableData = IntPtr.Zero
            Dim buffsize As Int32
            buffsize = 0
            GetTcpTable(tcptableData, buffsize, False)

            tcptableData = AllocHGlobal(buffsize)
            If GetTcpTable(tcptableData, buffsize, False) = NO_ERROR Then
                _tcptable.dwNumEntries = Math.Ceiling((buffsize - sizeof(Of Int32)()) / sizeof(Of MIB_TCPROW)())
                ReDim _tcptable.table(_tcptable.dwNumEntries - 1)
                Dim i As Int32
                For i = 0 To _tcptable.dwNumEntries - 1
                    Dim tempTableRow As IntPtr
                    tempTableRow = New IntPtr(tcptableData.ToInt32() + sizeof(Of Int32)() + i * sizeof(Of MIB_TCPROW)())
                    Dim tcpRow As MIB_TCPROW
                    tcpRow = New MIB_TCPROW()
                    PtrToStructure(tempTableRow, tcpRow)
                    _tcptable.table(i) = tcpRow
                Next

                Return True
            Else
                Return False
            End If
        End Function

        Public Class networkConnection
            Private _ipEnabled As Boolean = False
            Private _ipAddress As String = Nothing
            Private _ipSubnet As String = Nothing
            Private _defaultIpGateway As String = Nothing

            Public Function ipEnabled() As Boolean
                Return _ipEnabled
            End Function

            Public Function ipAddress() As String
                Return _ipAddress
            End Function

            Public Function ipSubnet() As String
                Return _ipSubnet
            End Function

            Public Function defaultIpGateway() As String
                Return _defaultIpGateway
            End Function

            Public Sub New(ByVal ipEnabled As Boolean, ByVal ipAddress As String, _
                           ByVal ipSubnet As String, ByVal defaultIpGateway As String)
                copy(_ipEnabled, ipEnabled)
                copy(_ipAddress, ipAddress)
                copy(_ipSubnet, ipSubnet)
                copy(_defaultIpGateway, defaultIpGateway)
            End Sub
        End Class

        Private _networkConnections() As networkConnection = Nothing

        Public Function networkConnections(Optional ByVal forceUpdate As Boolean = False) As networkConnection()
            If forceUpdate OrElse _networkConnections Is Nothing OrElse _networkConnections.Length() = 0 Then
                Dim mc As ManagementClass = Nothing
                mc = New ManagementClass("Win32_NetworkAdapterConfiguration")
                Dim nics As ManagementObjectCollection = Nothing
                nics = mc.GetInstances()
                ReDim _networkConnections(nics.Count() - 1)
                For i As Int64 = 0 To nics.Count() - 1
                    Dim ipEnabled As Boolean = False
                    Dim ipAddress As String = Nothing
                    Dim ipSubnet As String = Nothing
                    Dim defaultIpGateway As String = Nothing
                    ipEnabled = Convert.ToBoolean(nics(i)("IPEnabled"))
                    If ipEnabled Then
                        Try
                            ipAddress = CType(nics(i)("IPAddress"), String())(0)
                        Catch
                        End Try
                        Try
                            ipSubnet = CType(nics(i)("IPSubnet"), String())(0)
                        Catch
                        End Try
                        Try
                            defaultIpGateway = CType(nics(i)("DefaultIPGateway"), String())(0)
                        Catch
                        End Try
                    End If
                    _networkConnections(i) = New networkConnection(ipEnabled, ipAddress, ipSubnet, defaultIpGateway)
                Next
            End If

            Return _networkConnections
        End Function
    End Module
End Namespace
